package com.multi.web.controller.dataservice;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.multi.tenant.resp.LabelResp;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.multi.common.annotation.Log;
import com.multi.common.core.controller.BaseController;
import com.multi.common.core.domain.AjaxResult;
import com.multi.common.enums.BusinessType;
import com.multi.source.domain.BkDataBaseInfo;
import com.multi.source.service.IBkDataBaseInfoService;
import com.multi.common.utils.poi.ExcelUtil;
import com.multi.common.core.page.TableDataInfo;

/**
 * 数据库概要Controller
 *
 * @author pzj
 * @date 2024-11-20
 */
@RestController
@RequestMapping("/source/base")
public class BkDataBaseInfoController extends BaseController
{
    @Autowired
    private IBkDataBaseInfoService bkDataBaseInfoService;

    /**
     * 查询数据库概要列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BkDataBaseInfo bkDataBaseInfo)
    {
        startPage();
        List<BkDataBaseInfo> list = bkDataBaseInfoService.selectBkDataBaseInfoList(bkDataBaseInfo);
        return getDataTable(list);
    }

    /**
     * 导出数据库概要列表
     */
    @Log(title = "数据库概要", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BkDataBaseInfo bkDataBaseInfo)
    {
        List<BkDataBaseInfo> list = bkDataBaseInfoService.selectBkDataBaseInfoList(bkDataBaseInfo);
        ExcelUtil<BkDataBaseInfo> util = new ExcelUtil<BkDataBaseInfo>(BkDataBaseInfo.class);
        util.exportExcel(response, list, "数据库概要数据");
    }

    /**
     * 获取数据库概要详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bkDataBaseInfoService.selectBkDataBaseInfoById(id));
    }

    /**
     * 新增数据库概要
     */
    @Log(title = "数据库概要", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BkDataBaseInfo bkDataBaseInfo)
    {
        return toAjax(bkDataBaseInfoService.insertBkDataBaseInfo(bkDataBaseInfo));
    }

    /**
     * 修改数据库概要
     */
    @Log(title = "数据库概要", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BkDataBaseInfo bkDataBaseInfo)
    {
        return toAjax(bkDataBaseInfoService.updateBkDataBaseInfo(bkDataBaseInfo));
    }

    /**
     * 删除数据库概要
     */
    @Log(title = "数据库概要", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bkDataBaseInfoService.deleteBkDataBaseInfoByIds(ids));
    }

    @Log(title = "查询数据库IP列表信息",businessType = BusinessType.OTHER)
    @GetMapping("getDbIpOption")
    public AjaxResult getDbIpOption(){
        List<LabelResp> list= bkDataBaseInfoService.getDbIpOption();
        return AjaxResult.success(list);
    }

    @Log(title = "查询数据库端口列表信息",businessType = BusinessType.OTHER)
    @GetMapping("getDbPortOption")
    public AjaxResult getDbPortOption(String dbIp){
        if(dbIp.isEmpty()){
            return AjaxResult.success();
        }else{
            List<LabelResp> list= bkDataBaseInfoService.getDbPortOption(dbIp);
            return AjaxResult.success(list);
        }
    }
}
